package com.haizhi.crm.api.controller.sys.message;

import com.haizhi.crm.api.constant.MediaType;
import com.haizhi.crm.biz.constant.MessageStatus;
import com.haizhi.crm.biz.dao.MessageUserDao;
import com.haizhi.crm.biz.model.MessageUserDo;
import com.haizhi.crm.biz.model.MessageUserQo;
import com.haizhi.crm.biz.service.MessageService;
import com.haizhi.crm.biz.service.MessageUserService;
import com.haizhi.crm.common.model.Response;
import com.haizhi.crm.sys.auth.model.UserQo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by wangshan on 2018/5/15.
 */
@Api(description = "[业务-消息]消息增删改查" )
@RestController
@RequestMapping("/message" )
public class MessageController {

    @Autowired
    private MessageUserDao messageUserDao;

    @Autowired
    private MessageService messageService;

    @Autowired
    private MessageUserService messageUserService;

    @ApiOperation(value = "获取未读消息总数", notes = "输入：userId" )
    @GetMapping("/count" )
    public Response query(@ModelAttribute UserQo userQo) {
        int counts = messageUserDao.findCountByStatus(userQo.getUserId(), MessageStatus.UNREAD.value());
        return Response.success(counts);
    }

    @ApiOperation(value = "查看消息详情", notes = "输入：messageId" )
    @GetMapping("/detail" )
    public Response detail(@RequestParam("messageId" ) Long messageId) {
        if (messageService.detail(messageId) == null) {
            return Response.error("message is null" );
        }
        return Response.success(messageService.detail(messageId));
    }

    @ApiOperation(value = "查看实时消息详情", notes = "输入：messageId" )
    @GetMapping("/detail/actual" )
    public Response detailActual(@RequestParam("messageId" ) Long messageId) {
        if (messageService.detailActual(messageId) == null) {
            return Response.error("message is null" );
        }
        return Response.success(messageService.detailActual(messageId));
    }

    @ApiOperation(value = "全部消息标记未已读", notes = "输入：userId" )
    @GetMapping("/mark" )
    public Response mark(@ModelAttribute UserQo userQo) {
        messageUserDao.updateUserStatus(userQo.getUserId(), MessageStatus.READ.value());
        return Response.success();
    }

    @ApiOperation(value = "条件查询消息列表", notes = "输入：messageUserQo" )
    @PostMapping(path = "/list", consumes = MediaType.APP_JSON, produces = MediaType.APP_JSON)
    public Response query(@RequestBody MessageUserQo messageUserQo) {
        return Response.success(messageUserService.queryPage(messageUserQo));
    }

    @ApiOperation(value = "消息标记为已读", notes = "输入：messageUserDos" )
    @PostMapping(path = "/read", consumes = MediaType.APP_JSON, produces = MediaType.APP_JSON)
    public Response read(@RequestBody List<MessageUserDo> messageUserDos) {
        messageUserService.updateStatusByMessageId(messageUserDos);
        return Response.success();
    }

    @ApiOperation(value = "删除指定消息", notes = "输入：messageUserDos" )
    @DeleteMapping(path = "/delete" )
    public Response delete(@RequestBody List<MessageUserDo> messageUserDos) {
        messageUserService.deleteByMessageId(messageUserDos);
        return Response.success();
    }


}
